package com.machine.filling.function.set.orderDetail;

import android.content.Context;

import com.machine.filling.db.modle.TempOrderModle;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.util.DateUtils;
import com.machine.filling.util.SpUtils;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/4/11 0011.
 */
@Deprecated
public class OrderDetailPresenter implements OrderDetailContract.Presenter {
    private Context mContext;
    private OrderDetailContract.View mView;
    private TempOrderModle orderModle;

    public OrderDetailPresenter(Context mContext, OrderDetailContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        start();
    }

    @Override
    public void start() {
        orderModle = mView.getOrder();
    }

    @Override
    public void distoryContext() {

    }

    /**
     * 油卡加油 创建订单
     */
    @Override
    public void createCardFuelingOrder() {
        APIHttpManager.getInstance().createNewOrder(
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "2",
                "",
                Integer.parseInt(SpUtils.getInstance(mContext).getPingZhengHao(), 16) + "",
                new BaseHttpStringCallback() {
                    @Override
                    public void onSuccess(Call call, String result) {
                        //todo 订单更新订单状态
                        mView.showCreateCardFuelingOrderSuccess();
                        // TODO: 2018/5/24 0024  判断是否未支付, 未支付,调用支付接口
                    }

                    @Override
                    public void onFailed(String errorCode, String errorMsg, Exception exception) {
                        mView.showCreateCardFuelingOrderFailed();
                    }

                    @Override
                    public void onFinished() {

                    }
                }
        );
    }

    /**
     * 现金 支付
     */
    @Override
    public void payByCash() {

    }

    /**
     * 油卡 支付
     */
    @Override
    public void payByOilCard() {

    }

    /**
     * 提交预约订单
     */
    @Override
    public void createPreOrder() {
        APIHttpManager.getInstance().caretePreOrder(
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "2",
                DateUtils.getCurrentTimeYMDHMS(),
                new BaseHttpStringCallback() {
                    @Override
                    public void onSuccess(Call call, String result) {
                        //todo 订单更新订单状态

                        mView.showCreatePreOrderSuccess();

                    }

                    @Override
                    public void onFailed(String errorCode, String errorMsg, Exception exception) {
                        mView.showCreatePreOrderFailed();
                    }

                    @Override
                    public void onFinished() {

                    }
                }

        );
    }

    @Override
    public void uploadExceptionOrder() {
        // TODO: 2018/5/24 0024 ,先判断加油方式:
        // TODO: 1.油卡加油(a.客存:b.员工卡)[如果未创建订单,则先创建订单,后支付(客存为油卡支付,员工卡为现金支付)]--[如果已创建订单,直接调支付接口]
        // TODO: 2.预约加油 (未创建订单,应创建订单)
        String orderType = orderModle.getOrderType();//是否预约  1预约 0未预约
        if ("1".equals(orderType)) {
            //预约加油
            createPreOrder();
        } else {
            //是否上传订单 0 未上传 1已上传
            if ("0".equals(orderModle.getUploadState())) {
                //未上传, 先上传订单,然后,进行支付(用户卡:油卡支付;  员工卡:现金支付)
                createCardFuelingOrder();
            } else {
                //已上传,检查是否支付,未支付的(用户卡:油卡支付;  员工卡:现金支付)
                // 加油类型: 1用户的加油卡 2员工卡
                if ("1".equalsIgnoreCase(orderModle.getOilCardType())) {
                    payByOilCard();
                } else {
                    payByCash();
                }
            }
        }
    }

}
